<?php
/**
 * Copyright 2013 Marek Kaszewski
 *
 * This file is part of the JHUB1 Online Community Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Jhub1_Common\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Jhub1_Common\Model\SettingsObjects\AgentLocalConfigurationObject;
use Jhub1_Common\Model\SettingsObjects\AgentRemoteConfigurationObject;
use Jhub1_Common\Model\SettingsObjects\DateTimeObject;

/**
 * @ORM\Entity
 * @ORM\Table(name="jhoAgents", indexes={@ORM\Index(name="uuid_index", columns={"id"})})
 */
class Agents extends POBase
{

    /**
     * @ORM\Column(type="string", name="username", length=64, unique=true, nullable=false);
     * @var string
     * Generated by system jabber username - can not be changed - internal use only
     */
    private $username; //

    /**
     * @ORM\Column(type="string", name="name", length=25, nullable=false);
     * @var string
     * User friendly name - can be changed
     */
    private $name;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     * @var string
     * Presentation icon
     */
    private $icon;

    /**
     * @ORM\Column(type="string", length=100, nullable=true)
     * @var string
     * Associated colour
     */
    private $color;

    /**
     * @ORM\Column(type="string", name="description", length=60, nullable=true);
     * @var string
     * Created by user on the add agent page
     */
    private $description;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @var string
     * Stores serialized local config object
     */
    private $localConfig;

    /**
     * @ORM\Column(type="text", nullable=true)
     * @var string
     * Stores serialized remote agent configuration object
     */
    private $remoteConfig;

    /**
    * @ORM\Column(type="integer", name="sorder", nullable=false);
    * @var integer
    * List order - presentation purpose
    */
    private $sOrder;

    /**
     * @ORM\Column(type="string", name="buildid", nullable=true);
     * @var string
     * Information about build version
     */
    private $buildId;

    /**
     * @ORM\Column(type="string", name="builddate", length=20, nullable=true);
     * @var string
     * Information about build date
     */
    private $buildDate;

    /**
     * @ORM\ManyToOne(targetEntity="Users")
     * @var Users
     * @ORM\JoinColumns({
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     * })
     * Owner
     */
    private $user;

    /**
     * @ORM\OneToMany(targetEntity="AgentsEAV", mappedBy="agent", cascade={"persist","remove"})
     * @param \Doctrine\Common\Collections\Collection $property
     */
    private $eav;


    public function __construct(Users $user)
    {
        $this->eav = new \Doctrine\Common\Collections\ArrayCollection();
        $this->setUser($user);
        $this->setOrder(0);
        $this->setCreated(new DateTimeObject());
    }

    /** @param Users $user
     *  @throws InvalidArgumentException
     */
    public function setUser(Users $user)
    {
        if ($user === null || $user instanceof Users) {
            $this->user = $user;
        } else {
            throw new InvalidArgumentException('$user must be instance of Users or null!');
        }
    }

    public function getUser()
    {
        return $this->user;
    }

    /**
     * @param Agents $agent
     */
    public function addEAV(AgentsEAV $agentEAV)
    {
        $agentEAV->setOwner($this);
        $this->eav->add($agentEAV);
    }

    /** @return Collection */
    public function getEAVs()
    {
        return $this->eav;
    }

    public function getIcon()
    {
        return $this->icon;
    }

    public function setIcon($val)
    {
        $this->icon = $val;
    }

    public function getColor()
    {
        return $this->color;
    }

    public function setColor($val)
    {
        $this->color = $val;
    }

    public function getUserame()
    {
        return $this->username;
    }

    public function setUserame($val)
    {
        $this->username = $val;
    }

    public function getBuildId()
    {
        return $this->buildId;
    }

    public function setBuildId($val)
    {
        $this->buildId = $val;
    }

    public function getAgentName()
    {
        return $this->name;
    }

    public function setAgentName($val)
    {
        $this->name = $val;
    }

    public function getAgentDesc()
    {
        return $this->description;
    }

    public function setAgentDesc($val)
    {
        $this->description = $val;
    }

    public function getOrder()
    {
        return $this->sOrder;
    }

    public function setOrder($val)
    {
        $this->sOrder = $val;
    }

    public function getBuildDate()
    {
        $dateTime = new DateTimeObject();
        $dateTime->fromTimestamp($this->buildDate);
        return $dateTime;
    }

    public function setBuildDate(DateTimeObject $date)
    {
        $this->buildDate = $date->toTimestamp();
    }

    public function getLocalConfiguration()
    {
        $configuration = new AgentLocalConfigurationObject();
        $configuration->fromJSON($this->localConfig);
        return $configuration;
    }

    public function setLocalConfiguration(AgentLocalConfigurationObject $configurationObject)
    {
        $this->localConfig = $configurationObject->toJSON();
    }

    public function getRemoteConfiguration()
    {
        $configuration = new AgentRemoteConfigurationObject();
        $configuration->fromJSON($this->remoteConfig);
        return $configuration;
    }

    public function setRemoteConfiguration(AgentRemoteConfigurationObject $configurationObject)
    {
        $this->remoteConfig = $configurationObject->toJSON();
    }

    public function toString()
    {
        $string           = '---------------------------------------------'. PHP_EOL;
        $string = $string . 'AGENT OBJECT:                   ' . PHP_EOL;
        $string = $string . '---------------------------------------------'. PHP_EOL;
        $string = $string . 'ID:                       ' . $this->getId() . PHP_EOL;
        $string = $string . 'JUsername:                ' . $this->username . PHP_EOL;
        $string = $string . 'Name:                     ' . $this->name . PHP_EOL;
        $string = $string . 'Icon:                     ' . $this->icon . PHP_EOL;
        $string = $string . 'Colour:                   ' . $this->color . PHP_EOL;
        $string = $string . 'Description:              ' . $this->description . PHP_EOL;
        $string = $string . 'Order:                    ' . $this->sOrder . PHP_EOL;
        $string = $string . 'Created:                  ' . PHP_EOL . $this->getCreated()->toString() . PHP_EOL;
        $string = $string . 'Updated:                  ' . PHP_EOL . $this->getUpdated()->toString() . PHP_EOL;
        $string = $string . 'Local configuration:      ' . PHP_EOL . $this->getLocalConfiguration()->toString() . PHP_EOL;
        $string = $string . 'Remote configuration:     ' . PHP_EOL . $this->getRemoteConfiguration()->toString() . PHP_EOL;
        $string = $string . 'Build Id:                 ' . $this->buildId . PHP_EOL;
        $string = $string . 'Build Date:               ' . PHP_EOL . $this->getBuildDate()->toString() . PHP_EOL;
        $string = $string . '---------------------------------------------'. PHP_EOL;
        $string = $string . 'OWNER:                     ' . PHP_EOL;
        $string = $string . $this->getUser()->toString() . PHP_EOL;
        return $string;
    }
}
